package akbderby.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Persistent;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.Sort;

import akbderby.meta.QuestionMeta;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;

@Model(schemaVersion = 1)
public class Quiz implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(persistent = false)
    private InverseModelListRef<Question, Quiz> quesionListRef =
        new InverseModelListRef<Question, Quiz>(
            Question.class,
            QuestionMeta.get().quizRef.getName(),
            this,
            new Sort(
                QuestionMeta.get().questionNo.getName(),
                SortDirection.ASCENDING));

    @Persistent
    @Attribute(persistent = false)
    private UserData userData;

    @Persistent
    private Date createDate;

    @Persistent
    private String quizName;

    @Persistent
    private String contents;

    @Persistent
    private String passcd;

    @Persistent
    private boolean released = false;

    @NotPersistent
    private String searchWord;

    public void setUserData(UserData userData) {
        this.userData = userData;
    }

    public UserData getUserData() {
        return userData;
    }

    public String getQuizName() {
        return quizName;
    }

    public void setQuizName(String quizName) {
        this.quizName = quizName;
    }

    public String getPasscd() {
        return passcd;
    }

    public void setPasscd(String passcd) {
        this.passcd = passcd;
    }

    /**
     * Returns the key.
     * 
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     * 
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Quiz other = (Quiz) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public InverseModelListRef<Question, Quiz> getQuesionListRef() {
        return quesionListRef;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public String getContents() {
        return contents;
    }

    public void setReleased(boolean released) {
        this.released = released;
    }

    public boolean isReleased() {
        return released;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setSearchWord(String searchWord) {
        this.searchWord = searchWord;
    }

    public String getSearchWord() {
        this.searchWord =
            getUserData().getNickName()
                + " "
                + getQuizName()
                + " "
                + getContents();
        return searchWord;
    }
}
